﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using System.IO;

namespace pigs
{
    public class Achievments
    {
        public Achievment cityNoHits;
        public Achievment skNoHits;
        public Achievment bcNoHits;
        public Achievment cityAim;
        public Achievment skAim;
        public Achievment bcAim;
        public Achievment cityNoEscape;
        public Achievment skNoEscape;
        public Achievment bcNoEscape;
        public Achievment sur25;
        public Achievment sur50;
        public Achievment sur100;

        public Achievments()
        {
            cityNoHits = new Achievment("Beat the Ontario level with out taking damage");
            skNoHits = new Achievment("Beat the Saskatchewan level with out taking damage");
            bcNoHits = new Achievment("Beat the BC level with out taking damage");
            cityAim = new Achievment("Beat the Ontario level with accuracy of 90% or higher");
            skAim = new Achievment("Beat the Saskatchewan level with accuracy of 90% or higher");
            bcAim = new Achievment("Beat the BC level with accuracy of 90% or higher");
            cityNoEscape = new Achievment("Beat the Ontatio Level with no pigs escaping");
            skNoEscape = new Achievment("Beat the Saskatchewan Level with no pigs escaping");
            bcNoEscape = new Achievment("Beat the BC Level with no pigs escaping");
            sur25 = new Achievment("Beat wave 25 of survival mode");
            sur50 = new Achievment("Beat wave 50 of survival mode");
            sur100 = new Achievment("Beat wave 100 of survival mode");

            getSavedAchievments();
        }

        public void getSavedAchievments()
        {

            StreamReader reader;
            Boolean fileExists = true;

            try
            {
                reader = new StreamReader("achieve.txt", false);
                reader.Close();
            }
            catch
            {
                fileExists = false;
            }

            if (fileExists)
            {
                reader = new StreamReader("achieve.txt", false);
                try
                {
                    makeDefault();
                    if (toString(reader.ReadLine()).Equals("issac")) cityAim.achived = true;
                    if (toString(reader.ReadLine()).Equals("lucy")) cityNoHits.achived = true;
                    if (toString(reader.ReadLine()).Equals("curt")) cityNoEscape.achived = true;

                    if (toString(reader.ReadLine()).Equals("robyn")) skAim.achived = true;
                    if (toString(reader.ReadLine()).Equals("sam")) skNoHits.achived = true;
                    if (toString(reader.ReadLine()).Equals("jack")) skNoEscape.achived = true;

                    if (toString(reader.ReadLine()).Equals("bernard")) bcAim.achived = true;
                    if (toString(reader.ReadLine()).Equals("harlow")) bcNoHits.achived = true;
                    if (toString(reader.ReadLine()).Equals("tom")) bcNoEscape.achived = true;

                    if (toString(reader.ReadLine()).Equals("connie")) sur25.achived = true;
                    if (toString(reader.ReadLine()).Equals("eugene")) sur50.achived = true;
                    if (toString(reader.ReadLine()).Equals("whit")) sur100.achived = true;
                    
                    reader.Close();
                }
                catch
                {
                    reader.Close();
                    makeDefault();
                    saveAchievments();
                }
            }
            else
            {
                makeDefault();
                saveAchievments();
            }
        }

        public void makeDefault()
        {
            cityAim.achived = false;
            cityNoHits.achived = false;
            cityNoEscape.achived = false;

            bcAim.achived = false;
            bcNoHits.achived = false;
            bcNoEscape.achived = false;

            skAim.achived = false;
            skNoHits.achived = false;
            skNoEscape.achived = false;

            sur100.achived = false;
            sur50.achived = false;
            sur25.achived = false;
        }

        public void saveAchievments()
        {
            StreamWriter writer = new StreamWriter("achieve.txt", false);

            if (cityAim.achived) writer.WriteLine(toHex("issac"));
            else writer.WriteLine(toHex("false"));
            if (cityNoHits.achived) writer.WriteLine(toHex("lucy"));
            else writer.WriteLine(toHex("false"));
            if (cityNoEscape.achived) writer.WriteLine(toHex("curt"));
            else writer.WriteLine(toHex("false"));

            if (skAim.achived) writer.WriteLine(toHex("robyn"));
            else writer.WriteLine(toHex("false"));
            if (skNoHits.achived) writer.WriteLine(toHex("sam"));
            else writer.WriteLine(toHex("false"));
            if (skNoEscape.achived) writer.WriteLine(toHex("jack"));
            else writer.WriteLine(toHex("false"));

            if (bcAim.achived) writer.WriteLine(toHex("bernard"));
            else writer.WriteLine(toHex("false"));
            if (bcNoHits.achived) writer.WriteLine(toHex("harlow"));
            else writer.WriteLine(toHex("false"));
            if (bcNoEscape.achived) writer.WriteLine(toHex("tom"));
            else writer.WriteLine(toHex("false"));

            if (sur25.achived) writer.WriteLine(toHex("connie"));
            else writer.WriteLine(toHex("false"));
            if (sur50.achived) writer.WriteLine(toHex("eugene"));
            else writer.WriteLine(toHex("false"));
            if (sur100.achived) writer.WriteLine(toHex("whit"));
            else writer.WriteLine(toHex("false"));

            writer.Close();
        }

        public String toHex(String input)
        {
            string result = "";
            char[] values = input.ToCharArray();
            foreach (char letter in values)
            {
                // Get the integral value of the character.
                int value = Convert.ToInt32(letter);

                // Convert the decimal value to a hexadecimal value in string form.
                string hexOutput = String.Format("{0:X}", value);
                result = result + hexOutput + " ";
            }
            return result;
        }

        public string toString(String input)
        {
            String result = "";

            input = input.Substring(0, input.Length - 1);
            string[] inputSplit = input.Split(' ');
            foreach (String hex in inputSplit)
            {
                // Convert the number expressed in base-16 to an integer.
                int value = Convert.ToInt32(hex, 16);

                // Get the character corresponding to the integral value.
                result = result + Char.ConvertFromUtf32(value);
            }
            return result;
        }


    }
}
